package com.sky.task;

import com.alibaba.fastjson.JSON;
import com.sky.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class WebSocketTask {
    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 通过WebSocket每隔5秒向客户端发送消息
     */
    //@Scheduled(cron = "0/5 * * * * ?")
    public void sendMessageToClient() {
        log.info("通过WebSocket每隔5秒向客户端发送消息");
        webSocketServer.sendToAllClient("这是来自服务端的消息：" + DateTimeFormatter.ofPattern("HH:mm:ss").format(LocalDateTime.now()));
    }

    /**
     * 模拟来单提醒
     */
    //@Scheduled(cron = "30 * * * * ?") //每分钟第30秒触发一次
    public void testTX() {
        log.info("测试来单提醒");
        //通过WebSocket实现来单提醒，向客户端浏览器(项目管理端前台)推送消息
        Map map = new HashMap();
        map.put("type", 1); //消息类型，1表示来单提醒
        map.put("orderId", 1);
        map.put("content", "订单号：" + "测试提醒111");
        webSocketServer.sendToAllClient(JSON.toJSONString(map));
    }

}
